package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.LabelInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.LabelInfo;
import com.atguigu.lease.model.enums.ItemTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LOGIN
 * @title LabelController
 * @create 2025/6/23 16:55
 * @description
 */
@Tag(name="标签管理模块")
@RestController
@RequestMapping("/admin/label")
public class LabelController {
    @Autowired
    LabelInfoService labelInfoService;
    @Operation(summary = "1、查询所有标签")
    @GetMapping("/list")
    public Result list(
            @Parameter(description = "类型：1-公寓 2-房间",in = ParameterIn.QUERY, required = false)
            @RequestParam(value = "type" ,required = false) ItemTypeEnum type) {

        LambdaQueryWrapper<LabelInfo> wrapper = Wrappers.lambdaQuery(LabelInfo.class);
        //  参数一：布尔类型的值： true： 后面的条件会拼接， false：后面的条件会忽略
//        wrapper.eq(!StringUtils.isEmpty(type),LabelInfo::getType, type);
        //mp传入了 枚举对象，。mp可能不知到使用枚举对象的那个属性值当作查询条件参数值
        //使用枚举对象的 code 值当作查询值
        wrapper.eq(type!=null ,LabelInfo::getType, type);
        List<LabelInfo> labelInfos = labelInfoService.list(wrapper);
        return Result.ok(labelInfos);
    }

    //   /admin/label/saveOrUpdate
    @Operation(description = "2、更新或新增标签")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(
            @RequestBody LabelInfo labelInfo
    ){
        return Result.ok(labelInfoService.saveOrUpdate(labelInfo));
    }
//    /admin/label/deleteById?id=4
    @Operation(description = "3、根据id删除标签")
    @DeleteMapping("deleteById")
    public Result deleteById(
            @Parameter(description = "主键值")
            @RequestParam(value = "id",required = true)Long id
    ){

        return Result.ok(labelInfoService.removeById(id));
    }
}
